import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { addType, delType, getType, listType,  updateType } from "@/api/system/dict/type.js";

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'dictId'
  const moduleName =  '字典类型'
  const apiFn = {
    listApiFn: listType,
    delApiFn: delType,
    apiFnConfig:{
      info: getType,
      add: addType,
      edit: updateType
    }
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
  }
}

/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_normal_disable',
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: "字典名称",
      prop: "dictName",
      tableColumnProps: {
        align: "center"
      },
    },
    {
      label: "字典类型",
      prop: "dictType",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "状态",
      prop: "status",
      valueType: "select",
      options: dictRawData.sys_normal_disable,
      tableColumnProps: {
        align: "center"
      },
      propInForm:{
        valueType: "radio",
      }
    },

    {
      label: "备注", prop: "remark",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      },
      propInForm:{
        fieldProps:{
          type: "textarea",
        }
      }
    },
    {
      label: "创建时间",
      prop: "createTime",
      valueType: "date-picker",
      hideInForm:true,
      fieldProps: {type: "daterange", valueFormat: "YYYY-MM-DD"},
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作",
      prop: "action",
      width: 120,
      hideInSearch: true,
      hideInForm:true,
      tableColumnProps: {
        align: "center"
      }
    }
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  console.log(columns)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

